JPA Inheritance Mapping ব্যবহার করে আমরা ডাটাবেস টেবিলের মধ্যে Object-Oriented Programming এর Inheritance ধারণা প্রয়োগ করতে পারি। এটি বিশেষভাবে উপকারী যখন আমাদের কাছে Parent এবং Child ক্লাস থাকে এবং আমরা ডেটা টেবিলগুলোর মধ্যে সম্পর্ক তৈরি করতে চাই।
JPA বিভিন্ন স্ট্র্যাটেজি ব্যবহার করে Inheritance Mapping সম্পন্ন করে। এগুলো হলো:
- SINGLE_TABLE (ডিফল্ট স্ট্র্যাটেজি)
- TABLE_PER_CLASS
- JOINED
১. SINGLE_TABLE স্ট্র্যাটেজি
SINGLE_TABLE স্ট্র্যাটেজিতে Parent এবং Child ক্লাসের সব ডেটা একটি একক টেবিলে সংরক্ষিত হয়।
উদাহরণ
Entity ক্লাস (Parent এবং Child):
import jakarta.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Vehicle {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and Setters
}
@Entity
@DiscriminatorValue("Car")
public class Car extends Vehicle {
private int seatCount;
// Getters and Setters
}
@Entity
@DiscriminatorValue("Bike")
public class Bike extends Vehicle {
private boolean hasCarrier;
// Getters and Setters
}
জেনারেট হওয়া টেবিল:
| id | name | type | seatCount | hasCarrier |
|---|---|---|---|---|
| 1 | Toyota | Car | 5 | NULL |
| 2 | Yamaha | Bike | NULL | true |
২. TABLE_PER_CLASS স্ট্র্যাটেজি
TABLE_PER_CLASS স্ট্র্যাটেজিতে Parent ক্লাস এবং প্রতিটি Child ক্লাসের জন্য আলাদা আলাদা টেবিল তৈরি হয়।
উদাহরণ
Entity ক্লাস (Parent এবং Child):
import jakarta.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Vehicle {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and Setters
}
@Entity
public class Car extends Vehicle {
private int seatCount;
// Getters and Setters
}
@Entity
public class Bike extends Vehicle {
private boolean hasCarrier;
// Getters and Setters
}
জেনারেট হওয়া টেবিল:
Car টেবিল:
| id | name | seatCount |
|---|---|---|
| 1 | Toyota | 5 |
Bike টেবিল:
| id | name | hasCarrier |
|---|---|---|
| 1 | Yamaha | true |
৩. JOINED স্ট্র্যাটেজি
JOINED স্ট্র্যাটেজিতে Parent ক্লাস এবং Child ক্লাসের জন্য আলাদা টেবিল তৈরি হয়, এবং তাদের মধ্যে একটি JOIN সম্পর্ক থাকে।
উদাহরণ
Entity ক্লাস (Parent এবং Child):
import jakarta.persistence.*;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Vehicle {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and Setters
}
@Entity
public class Car extends Vehicle {
private int seatCount;
// Getters and Setters
}
@Entity
public class Bike extends Vehicle {
private boolean hasCarrier;
// Getters and Setters
}
জেনারেট হওয়া টেবিল:
Vehicle টেবিল:
| id | name |
|---|---|
| 1 | Toyota |
| 2 | Yamaha |
Car টেবিল:
| id | seatCount |
|---|---|
| 1 | 5 |
Bike টেবিল:
| id | hasCarrier |
|---|---|
| 2 | true |
Service এবং Repository উদাহরণ
Repository:
import org.springframework.data.jpa.repository.JpaRepository;
public interface VehicleRepository extends JpaRepository<Vehicle, Long> {
}
Service:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class VehicleService {
@Autowired
private VehicleRepository vehicleRepository;
public void saveVehicles() {
Car car = new Car();
car.setName("Toyota");
car.setSeatCount(5);
vehicleRepository.save(car);
Bike bike = new Bike();
bike.setName("Yamaha");
bike.setHasCarrier(true);
vehicleRepository.save(bike);
}
}
Inheritance স্ট্র্যাটেজি বেছে নেওয়ার পরামর্শ
- SINGLE_TABLE: পারফরম্যান্স ভালো এবং টেবিল কম প্রয়োজন। তবে অনেক ক্ষেত্রেই NULL ভ্যালু তৈরি হয়।
- TABLE_PER_CLASS: আলাদা টেবিল প্রয়োজন এবং Parent ক্লাসের ডেটা Child টেবিলে মাইগ্রেট হয়।
- JOINED: ডেটা নরমালাইজড এবং ডেটার রিপিটেশন নেই, তবে
JOINঅপারেশনের কারণে স্লো হতে পারে।
সারাংশ
JPA Inheritance Mapping ব্যবহার করে Parent-Child সম্পর্কের ডেটা টেবিলের সঙ্গে মানিয়ে নেওয়া যায়। SINGLE_TABLE, TABLE_PER_CLASS, এবং JOINED স্ট্র্যাটেজির মধ্যে সঠিক একটি নির্বাচন অ্যাপ্লিকেশনের প্রয়োজন অনুযায়ী নির্ভর করে। প্রতিটি স্ট্র্যাটেজির নিজস্ব সুবিধা এবং সীমাবদ্ধতা রয়েছে।
Read more